// 排行榜页面逻辑
const app = getApp()

Page({
  data: {
    leaderboard: [],
    loading: false
  },

  onLoad() {
    this.getLeaderboard()
  },

  onShow() {
    this.getLeaderboard()
  },

  // 获取排行榜数据
  getLeaderboard() {
    this.setData({ loading: true })
    
    wx.request({
      url: `${app.globalData.baseUrl}/api/leaderboard/list`,
      method: 'GET',
      success: (res) => {
        if (res.data.success) {
          this.setData({ 
            leaderboard: res.data.data,
            loading: false 
          })
        } else {
          this.setData({ loading: false })
          wx.showToast({
            title: res.data.message || '获取排行榜失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        console.error('获取排行榜失败:', err)
        this.setData({ loading: false })
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        })
      }
    })
  },

  // 返回我的页面
  returnToProfile() {
    wx.navigateBack({
      delta: 1
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.getLeaderboard()
    wx.stopPullDownRefresh()
  },

  // 分享给好友
  async onShareAppMessage() {
    const ShareUtil = require('../../utils/shareUtil')
    return await ShareUtil.generateShareAppMessageConfig('leaderboard', {
      leaderboard: this.data.leaderboard
    })
  },

  // 分享到朋友圈
  async onShareTimeline() {
    const ShareUtil = require('../../utils/shareUtil')
    return await ShareUtil.generateShareTimelineConfig('leaderboard', {
      leaderboard: this.data.leaderboard
    })
  }
})
